package pcjavanet.mobilesell.web.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import pcjavanet.mobilesell.model.Employee;
import pcjavanet.mobilesell.model.PageBean;
import pcjavanet.mobilesell.model.QryCondition;
import pcjavanet.mobilesell.service.EmployeeService;

import com.opensymphony.xwork2.ActionSupport;
@Namespace("/employee")
@Controller
@Scope("prototype")
@ParentPackage("json-default")
@Results({
	@Result(name = "success", location = "/view/Employee.jsp"),
	@Result(name = "input", location = "/view/Employee.jsp")
})

public class EmployeeAction  extends ActionSupport {

  @Resource
  private EmployeeService employeeService;
  private   java.lang.Integer dbid ;
  private   java.lang.String name ;
  private   java.lang.String password ;
  private   java.lang.String oldpassword ;
  private   java.lang.Integer refDepartmentId ;
  private   java.lang.Integer refShopId ;
  private   java.util.Date birthday ;
  private   java.lang.String sex ;
  private   java.lang.String hometown ;
  private   java.lang.String tele ;
  private   java.lang.String mobile ;
  private   java.lang.String status ;
  private   java.util.Date createDate ;
  private int start  ;
  private int limit  ;
  private int totalProperty  ;
  private boolean success = true ;
  private String  info ;
  private List<Employee>  employeePageBeans  ;
  public    java.lang.Integer getDbid() {
   	return this.dbid;
  }
  public java.lang.String getOldpassword() {
	return oldpassword;
}
public void setOldpassword(java.lang.String oldpassword) {
	this.oldpassword = oldpassword;
}
public void setDbid(  java.lang.Integer  dbid) {
   	 this.dbid=dbid;
  }
  public    java.lang.String getName() {
   	return this.name;
  }
  public void setName(  java.lang.String  name) {
   	 this.name=name;
  }
  public java.lang.Integer getRefShopId() {
	return refShopId;
	}
	public void setRefShopId(java.lang.Integer refShopId) {
		this.refShopId = refShopId;
	}
public    java.lang.String getPassword() {
   	return this.password;
  }
  public void setPassword(  java.lang.String  password) {
   	 this.password=password;
  }
  public    java.lang.Integer getRefDepartmentId() {
   	return this.refDepartmentId;
  }
  public void setRefDepartmentId(  java.lang.Integer  refDepartmentId) {
   	 this.refDepartmentId=refDepartmentId;
  }
  public    java.util.Date getBirthday() {
   	return this.birthday;
  }
  public void setBirthday(  java.util.Date  birthday) {
   	 this.birthday=birthday;
  }
  public    java.lang.String getSex() {
   	return this.sex;
  }
  public void setSex(  java.lang.String  sex) {
   	 this.sex=sex;
  }
  public    java.lang.String getHometown() {
   	return this.hometown;
  }
  public void setHometown(  java.lang.String  hometown) {
   	 this.hometown=hometown;
  }
  public    java.lang.String getTele() {
   	return this.tele;
  }
  public void setTele(  java.lang.String  tele) {
   	 this.tele=tele;
  }
  public    java.lang.String getMobile() {
   	return this.mobile;
  }
  public void setMobile(  java.lang.String  mobile) {
   	 this.mobile=mobile;
  }
  public    java.lang.String getStatus() {
   	return this.status;
  }
  public void setStatus(  java.lang.String  status) {
   	 this.status=status;
  }
  public    java.util.Date getCreateDate() {
   	return this.createDate;
  }
  public void setCreateDate(  java.util.Date  createDate) {
   	 this.createDate=createDate;
  }
  public  void setStart(int start) {
   	 this.start = start;
  }
  public  void setLimit(int limit) {
   	 this.limit = limit;
  }
  public  void setTotalProperty(int totalProperty) {
   	 this.totalProperty = totalProperty;
  }
  public  void setEmployeePageBeans(List<Employee> employeePageBeans){
   	 this.employeePageBeans =employeePageBeans  ;
  }
  public  int getStart( ) {
   	 return this.start;
  }
  public  int getLimit( ) {
   	 return this.limit;
  }
  public  int getTotalProperty( ) {
   	 return this.totalProperty;
  }
  public    List<Employee> getEmployeePageBeans(){
   	 return this.employeePageBeans   ;
  }
  public  String getInfo( ) {
   	 return this.info;
  }
  public  void setInfo(String info){
   	 this.info = info ;
  }
  public  boolean getSuccess( ) {
   	 return this.success;
  }
  public  void setSuccess(boolean success){
   	 this.success = success ;
  }

  @Action(value="insertEmployee",results = {@Result(name = "success", type = "json")})
  public String  insertEmployee() throws Exception{
   	success = false ;
   	info = "" ;
  	Employee bean = new Employee();
  	bean.setName(this.name); 
  	bean.setPassword(this.password); 
  	bean.setRefDepartmentId(this.refDepartmentId); 
  	bean.setBirthday(this.birthday); 
  	bean.setSex(this.sex); 
  	bean.setHometown(this.hometown); 
  	bean.setTele(this.tele); 
  	bean.setMobile(this.mobile); 
  	bean.setStatus(this.status); 
  	bean.setCreateDate(this.createDate); 
  	bean.setRefShopId(refShopId);
	employeeService.insert(bean);
   	success = true ;
   	info = "Add Successful" ;
   	return SUCCESS;
  }

  @Action(value="queryEmployee",results = {@Result(name = "success", type = "json")})
  public String  queryEmployee() throws Exception{
  	Employee bean = new Employee();
  	bean.setDbid(this.dbid); 
  	bean.setName(this.name); 
  	bean.setPassword(this.password); 
  	bean.setRefDepartmentId(this.refDepartmentId); 
  	bean.setBirthday(this.birthday); 
  	bean.setSex(this.sex); 
  	bean.setHometown(this.hometown); 
  	bean.setTele(this.tele); 
  	bean.setMobile(this.mobile); 
  	bean.setStatus(this.status); 
  	bean.setCreateDate(this.createDate); 
  	bean.setRefShopId(refShopId);
	employeeService.selectByPrimaryKey(bean);
   	return SUCCESS;
  }
  
  @Action(value="loginCheck",results = {@Result(name = "success", type = "json")})
  public String  loginEmployee() throws Exception{
	  Employee bean = new Employee();
	  bean.setDbid(this.dbid); 
	  bean.setName(this.name); 
	  bean.setPassword(this.password); 
	  bean.setRefDepartmentId(this.refDepartmentId); 
	  bean.setBirthday(this.birthday); 
	  bean.setSex(this.sex); 
	  bean.setHometown(this.hometown); 
	  bean.setTele(this.tele); 
	  bean.setMobile(this.mobile); 
	  bean.setStatus(this.status); 
	  bean.setCreateDate(this.createDate); 
	  bean.setRefShopId(refShopId);
	  List<Employee> ls = employeeService.checkLogin(bean);
	 if ( ls.size() > 0 ) {
		 success = true ; 
		 HttpSession session = ServletActionContext.getRequest().getSession();
		 bean = ls.get(0);
		 session.setAttribute("employee", bean);
	 }
	 else 
		 success = false ;
	  return SUCCESS;
  }
  @Action(value="logout",results = {@Result(name = "success", type = "json")})
  public String  logoutEmployee() throws Exception{
		  HttpSession session = ServletActionContext.getRequest().getSession();
		  if ( session.getAttribute("employee") != null )
			  session.removeAttribute("employee" );
	  return SUCCESS;
  }

  @Action(value="updateEmployee",results = {@Result(name = "success", type = "json")})
  public String  updateEmployee() throws Exception{
   	success = false ;
   	info = "" ;
  	Employee bean = new Employee();
  	bean.setDbid(this.dbid); 
  	bean.setName(this.name); 
  	bean.setPassword(this.password); 
  	bean.setRefDepartmentId(this.refDepartmentId); 
  	bean.setBirthday(this.birthday); 
  	bean.setSex(this.sex); 
  	bean.setHometown(this.hometown); 
  	bean.setTele(this.tele); 
  	bean.setMobile(this.mobile); 
  	bean.setStatus(this.status); 
  	bean.setCreateDate(this.createDate); 
  	bean.setRefShopId(refShopId);
  	bean =employeeService.selectByPrimaryKey(bean);
  	bean.setDbid(this.dbid); 
  	bean.setName(this.name); 
//  	bean.setPassword(this.password); 
  	bean.setRefDepartmentId(this.refDepartmentId); 
  	bean.setBirthday(this.birthday); 
  	bean.setSex(this.sex); 
  	bean.setHometown(this.hometown); 
  	bean.setTele(this.tele); 
  	bean.setMobile(this.mobile); 
  	bean.setStatus(this.status); 
  	bean.setCreateDate(this.createDate); 
  	bean.setRefShopId(refShopId);
	employeeService.updateByPrimaryKeySelective(bean);
   	success = true ;
   	info = "Update Successful" ;
   	return SUCCESS;
  }

  @Action(value="deleteEmployee",results = {@Result(name = "success", type = "json")})
  public String  deleteEmployee() throws Exception{
  	Employee bean = new Employee();
  	bean.setDbid(this.dbid); 
  	bean.setName(this.name); 
  	bean.setPassword(this.password); 
  	bean.setRefDepartmentId(this.refDepartmentId); 
  	bean.setBirthday(this.birthday); 
  	bean.setSex(this.sex); 
  	bean.setHometown(this.hometown); 
  	bean.setTele(this.tele); 
  	bean.setMobile(this.mobile); 
  	bean.setStatus(this.status); 
  	bean.setCreateDate(this.createDate); 
	employeeService.deleteByPrimaryKey(bean);
   	return SUCCESS;
  }

  @Action(value="listEmployee",results = {@Result(name = "success", type = "json")})
  public String  listEmployee() throws Exception{
 	QryCondition qry = RequestUtil.getQryCondition(   (javax.servlet.http.HttpServletRequest)com.opensymphony.xwork2.ActionContext.getContext().get(org.apache.struts2.ServletActionContext.HTTP_REQUEST)); 	PageBean pageBean = new PageBean();
	pageBean.setPageSize( limit );
	pageBean.setStartCount( start );
	totalProperty =employeeService.getTotalRecords( qry, pageBean );
	employeePageBeans =employeeService.getOnePageBeans( qry, pageBean );
   	return SUCCESS;
  }

  @Action("index")
  public String index() throws Exception{
   	return SUCCESS;
  }
  
  
  @Action(value="updatePassword",results = {@Result(name = "success", type = "json" /*,location = "/view/displayUser.jsp"*/)})
  public String updatePassword() throws Exception{
	 	success = false ;
	   	info = "" ;
	   	Employee bean = new Employee();
	   	bean.setDbid(this.dbid); 
	   	bean.setPassword(this.oldpassword); 
	   List<Employee> ls  = employeeService.checkByDbidAndPassword( bean );
	   	if ( ls.size() > 0 ) {
	   		bean = ls.get(0);
	   		bean.setPassword(this.password); 
	   		employeeService.updateByPrimaryKeySelective(bean);
	   		success = true ;
	   	}
	  return SUCCESS;
  }
  
  @Action(value="displayUser",results = {@Result(name = "success",location = "/view/displayUser.jsp")})
  public String displayUser() throws Exception{
	  return SUCCESS;
  }
}
